Skip to content

276. Paint Fence#16

Open
katsukii wants to merge 1 commit intomainfrom
276_paint
Open

276. Paint Fence#16
katsukii wants to merge 1 commit intomainfrom
276_paint

Conversation

@katsukii
Copy link
Owner

@katsukii katsukii commented Feb 6, 2025

問題

https://leetcode.com/problems/paint-fence/description/

Question:
You are painting a fence of n posts with k different colors. You must paint the posts following these rules:

Every post must be painted exactly one color.
There cannot be three or more consecutive posts with the same color.
Given the two integers n and k, return the number of ways you can paint the fence.

言語

Java

次に解く問題

22. Generate Parentheses

Comment on lines +130 to +131
long dp1 = k * k; // dp(2)
long dp2 = k; // dp(1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この dp、dynamic programming でさえない気がしますね。

long dp1 = k * k; // dp(2)
long dp2 = k; // dp(1)

return (int)(result[0][0] * dp1 + result[0][1] * dp2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体 int でいいのではないでしょうか。意味が分かっているならばともかく「とりあえず long」みたいなのはあまり好まれないでしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

お手本を写経しながら練習していたため深く考えずこうなっておりました。以後気をつけます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants